const Banner = require('@models/banner');
const { Op } = require('sequelize');

// 获取轮播图列表
exports.list = async (req, res, next) => {
  try {
    const { keyword, status, page = 1, pageSize = 10 } = req.query;
    const where = {};
    const offset = (parseInt(page) - 1) * parseInt(pageSize);
    const limit = parseInt(pageSize);

    if (keyword) {
      where.title = {
        [Op.like]: `%${keyword}%`
      };
    }

    if (status !== undefined) {
      where.status = status;
    }

    // 查询总数
    const count = await Banner.count({ where });

    // 分页查询数据
    const banners = await Banner.findAll({
      where,
      order: [
        ['sort', 'ASC'],
        ['id', 'DESC']
      ],
      offset,
      limit
    });

    res.json({
      code: 200,
      message: '获取成功',
      data: banners,
      total: count,
      page: parseInt(page),
      pageSize: parseInt(pageSize)
    });
  } catch (err) {
    next(err);
  }
};

// 创建轮播图
exports.create = async (req, res, next) => {
  try {
    const { title, image, link, sort = 0, status = 1 } = req.body;

    const banner = await Banner.create({
      title,
      image,
      link,
      sort,
      status
    });

    res.json({
      code: 200,
      message: '创建成功',
      data: banner
    });
  } catch (err) {
    next(err);
  }
};

// 更新轮播图
exports.update = async (req, res, next) => {
  try {
    const { id } = req.params;
    const { title, image, link, sort, status } = req.body;

    const banner = await Banner.findByPk(id);
    if (!banner) {
      return res.status(404).json({
        code: 404,
        message: '轮播图不存在'
      });
    }

    await banner.update({
      title,
      image,
      link,
      sort,
      status
    });

    res.json({
      code: 200,
      message: '更新成功'
    });
  } catch (err) {
    next(err);
  }
};

// 删除轮播图
exports.delete = async (req, res, next) => {
  try {
    const { id } = req.params;

    const banner = await Banner.findByPk(id);
    if (!banner) {
      return res.status(404).json({
        code: 404,
        message: '轮播图不存在'
      });
    }

    await banner.destroy();

    res.json({
      code: 200,
      message: '删除成功'
    });
  } catch (err) {
    next(err);
  }
};

// 更新轮播图状态
exports.updateStatus = async (req, res, next) => {
  try {
    const { id } = req.params;
    const { status } = req.body;

    const banner = await Banner.findByPk(id);
    if (!banner) {
      return res.status(404).json({
        code: 404,
        message: '轮播图不存在'
      });
    }

    await banner.update({ status });

    res.json({
      code: 200,
      message: '状态更新成功'
    });
  } catch (err) {
    next(err);
  }
};

// 更新轮播图排序
exports.updateSort = async (req, res, next) => {
  try {
    const { id } = req.params;
    const { sort } = req.body;

    console.log('排序请求参数:', { id, sort });
    console.log('请求体:', req.body);

    const banner = await Banner.findByPk(id);
    console.log('查询到的轮播图:', banner);
    
    if (!banner) {
      console.log('轮播图未找到:', id);
      return res.status(404).json({
        code: 404,
        message: '轮播图不存在'
      });
    }

    await banner.update({ sort });

    res.json({
      code: 200,
      message: '排序更新成功'
    });
  } catch (err) {
    console.error('排序更新出错:', err);
    next(err);
  }
}; 